package jzqb.controller.admin;

import haha.life.common.CutPageBean;
import haha.life.common.MonitorTypeTool;
import haha.life.common.PermissionTypeTool;
import haha.life.common.SysTool;
import haha.life.normal.BeanToMapUtils;
import haha.life.normal.ExcelUtils;
import haha.life.normal.TokenUtils;
import io.swagger.annotations.ApiOperation;
import jzqb.annotation.PermissionCheck.PermissionCheck;
import jzqb.bo.AdminQueryBo;
import jzqb.bo.MonitorBo;
import jzqb.common.Result;
import jzqb.po.AdminLoginLogPo;
import jzqb.po.AdminViewDownLogPo;
import jzqb.po.MonitorPo;
import jzqb.po.ZYPo;
import jzqb.service.AdminLogService;
import jzqb.vo.StandardStatisticsVo;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.websocket.server.PathParam;
import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/adminLog")
public class AdminLogController {

    @Autowired
    private AdminLogService adminLogService;

    @ApiOperation(value = "标准统计")
    @GetMapping("/StandardStatistics")
    public Result StandardStatistics(@RequestParam("page") int page,
                                     @RequestParam("pageSize") int pageSize,
                                     @RequestParam("searchWay") String searchWay,
                                     @RequestParam("keyWord") String keyWord,
                                     @RequestParam("groupWay") Integer groupWay,
                                     @RequestParam("isExport") Integer isExport,
                                     @RequestParam("orderBy") String orderBy,
                                     @RequestParam("beginDate") String beginDate,
                                     @RequestParam("endDate") String endDate,
                                     @RequestParam("dbCode") String dbCode){
        AdminQueryBo bo = new AdminQueryBo();
        bo.setSearchWay(searchWay);
        bo.setKeyWord(keyWord);
        bo.setStart((page - 1) * pageSize);
        bo.setEnd(pageSize);
        bo.setDbCode(dbCode);
        bo.setGroupWay(groupWay);
        bo.setBeginDate(beginDate);
        bo.setEndDate(endDate);
        bo.setOrderBy(orderBy);
        bo.setIsExport(isExport);
        Integer total = adminLogService.count(bo).intValue();
        List<StandardStatisticsVo> list = adminLogService.StandardStatistics(bo);
        CutPageBean cutPageBean = new CutPageBean<>();
        cutPageBean.initCutPage(total, pageSize, list);
        return Result.succ(cutPageBean);
    }

    @ApiOperation(value = "导出标准统计")
    @GetMapping("/exportStandardStatistics")
    public void exportStandardStatistics(
            @RequestParam("searchWay") String searchWay,
            @RequestParam("keyWord") String keyWord,
            @RequestParam("groupWay") Integer groupWay,
            @RequestParam("isExport") Integer isExport,
            @RequestParam("orderBy") String orderBy,
            @RequestParam("beginDate") String beginDate,
            @RequestParam("endDate") String endDate,
            @RequestParam("dbCode") String dbCode, HttpServletResponse response) throws Exception {
        AdminQueryBo bo = new AdminQueryBo();
        bo.setSearchWay(searchWay);
        bo.setKeyWord(keyWord);
        bo.setDbCode(dbCode);
        bo.setGroupWay(groupWay);
        bo.setBeginDate(beginDate);
        bo.setEndDate(endDate);
        bo.setOrderBy(orderBy);
        bo.setIsExport(isExport);
        Workbook wb = null;
        String s = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        List<StandardStatisticsVo> list = adminLogService.exportStandardStatistics1(bo);
        List<Map<String, Object>> maps = BeanToMapUtils.convertListBean2ListMap(list, StandardStatisticsVo.class);
        if (groupWay == 1) {
            String[] excelHeader = {"序号","题名", "来源数据库", "作者", "发表时间", "浏览量", "下载量"};
            String[] excelHeaderKey = {"id","title", "dbsource", "author", "publishdate", "browse", "download"};
            wb = ExcelUtils.exportExcel(excelHeader, excelHeaderKey, maps, s+"资源统计");
        } else if (groupWay == 2) {
            String[] excelHeader = {"序号", "来源数据库", "资源量", "浏览量", "下载量"};
            String[] excelHeaderKey = {"id",  "dbsource", "totalRESOURCE","totalBROWSERATE", "totalDOWNLOADRATE"};
            wb = ExcelUtils.exportExcel(excelHeader, excelHeaderKey, maps, s+"资源统计");
        }
        String title = s+"资源统计.xlsx";
        ExcelUtils.setExcelResponse(response, title, wb);
    }


    @GetMapping("loglist")
    public Result layoutname(
            @PathParam("groupType") String groupType,
            @PathParam("groupValue") String groupValue,
            @PathParam("source") String source,
            @PathParam("accessType") String accessType,
            @PathParam("orderType") String orderType,
            @PathParam("orderValue") String orderValue,
            @PathParam("page") int page,
            @PathParam("pageSize") int pageSize,
            @PathParam("startTime") String startTime,
            @PathParam("endTime") String endTime
    ) {
        AdminViewDownLogPo adminViewDownLogPo = new AdminViewDownLogPo();
        adminViewDownLogPo.setGroupType(groupType);
        adminViewDownLogPo.setGroupValue(groupValue);
        adminViewDownLogPo.setSource("");
        adminViewDownLogPo.setAccessType(accessType);
        adminViewDownLogPo.setOrderType(orderType);
        adminViewDownLogPo.setOrderValue(orderValue);
        adminViewDownLogPo.setPage(page);
        adminViewDownLogPo.setPageSize(pageSize);
        adminViewDownLogPo.setStartPage((page-1)*pageSize);
        adminViewDownLogPo.setStartTime(startTime);
        adminViewDownLogPo.setEndTime(endTime);
        CutPageBean<AdminViewDownLogPo> cutPageBean = adminLogService.cutPage(adminViewDownLogPo);
        return Result.succ(cutPageBean);
    }


    @ApiOperation(value = "导出浏览下载日志")
    @GetMapping("/exportViewDownloadLog")
    public void exportViewDownloadLog(
            @PathParam("groupType") String groupType,
            @PathParam("groupValue") String groupValue,
            @PathParam("source") String source,
            @PathParam("accessType") String accessType,
            @PathParam("orderType") String orderType,
            @PathParam("orderValue") String orderValue,
            @PathParam("startTime") String startTime,
            @PathParam("endTime") String endTime,
            HttpServletResponse response
    ) throws Exception {
        AdminViewDownLogPo adminViewDownLogPo = new AdminViewDownLogPo();
        adminViewDownLogPo.setGroupType(groupType);
        adminViewDownLogPo.setGroupValue(groupValue);
        adminViewDownLogPo.setSource("");
        adminViewDownLogPo.setAccessType(accessType);
        adminViewDownLogPo.setStartTime(startTime);
        adminViewDownLogPo.setEndTime(endTime);
        if ("".equals(groupType)){
            adminViewDownLogPo.setOrderType("创建时间");
        }else{
            adminViewDownLogPo.setOrderType(orderType);
        }
        adminViewDownLogPo.setOrderValue(orderValue);
        Workbook wb = null;
        String s = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        List<AdminViewDownLogPo> list = adminLogService.getResNotPaged(adminViewDownLogPo);
        List<Map<String, Object>> maps = BeanToMapUtils.convertListBean2ListMap(list, AdminViewDownLogPo.class);

//        定义表头字段


        if ("".equals(groupType)){
            String[] excelHeader = {"用户ID","姓名","资源名称", "资源来源", "访问类型","创建时间"};
            String[] excelHeaderKey = {"userID","userName","fileName", "source", "accessType","createTime"};
            wb = ExcelUtils.exportExcel(excelHeader, excelHeaderKey, maps, s+"浏览下载统计");
        }
        else if ("worknum".equals(groupType)){
            String[] excelHeader = {"用户ID","姓名","合计"};
            String[] excelHeaderKey = {"userID","userName","count"};
            wb = ExcelUtils.exportExcel(excelHeader, excelHeaderKey, maps, s+"浏览下载统计");
        }
        else if ("filename".equals(groupType)){
            String[] excelHeader = {"资源名称","合计"};
            String[] excelHeaderKey = {"fileName","count"};
            wb = ExcelUtils.exportExcel(excelHeader, excelHeaderKey, maps, s+"浏览下载统计");
        }else{
            String[] excelHeader = {"用户ID","资源名称", "资源来源", "访问类型","合计"};
            String[] excelHeaderKey = {"userID","fileName", "source", "accessType","count"};
            wb = ExcelUtils.exportExcel(excelHeader, excelHeaderKey, maps, s+"浏览下载统计");
        }

//        String[] excelHeader = {"用户ID","资源名称", "资源来源", "访问类型","合计"};
//        String[] excelHeaderKey = {"userID","fileName", "source", "accessType","count"};
//        wb = ExcelUtils.exportExcel(excelHeader, excelHeaderKey, maps, s+"浏览下载统计");

        String title = s+"浏览下载统计.xlsx";
        ExcelUtils.setExcelResponse(response, title, wb);
    }

    /*****************************登录日志*******************************/
    @ApiOperation(value = "查看登录日志")
    @GetMapping("/LoginLogList")
    public Result LoginLogList(
            @PathParam("groupType") String groupType,
            @PathParam("groupValue") String groupValue,
            @PathParam("startTime") String startTime,
            @PathParam("endTime") String endTime,
            @PathParam("orderType") String orderType,
            @PathParam("orderValue") String orderValue,
            @PathParam("page") int page,
            @PathParam("pageSize") int pageSize
    ) {
        AdminLoginLogPo adminLoginLogPo = new AdminLoginLogPo();
        adminLoginLogPo.setGroupType(groupType);
        adminLoginLogPo.setGroupValue(groupValue);
        adminLoginLogPo.setStartTime(startTime);
        adminLoginLogPo.setEndTime(endTime);
        adminLoginLogPo.setOrderType(orderType);
        adminLoginLogPo.setOrderValue(orderValue);
        adminLoginLogPo.setPage(page);
        adminLoginLogPo.setPageSize(pageSize);
        adminLoginLogPo.setStartPage((page-1)*pageSize);
        CutPageBean<AdminLoginLogPo> cutPageBean = adminLogService.cutPage_login(adminLoginLogPo);
        return Result.succ(cutPageBean);
    }


    @ApiOperation(value = "导出登录日志")
    @GetMapping("/exportLoginLog")
    public void exportLoginLog(
            @PathParam("groupType") String groupType,
            @PathParam("groupValue") String groupValue,
            @PathParam("startTime") String startTime,
            @PathParam("endTime") String endTime,
            @PathParam("orderType") String orderType,
            @PathParam("orderValue") String orderValue,
            HttpServletResponse response
    ) throws Exception {
        AdminLoginLogPo adminLoginLogPo = new AdminLoginLogPo();
        adminLoginLogPo.setGroupType(groupType);
        adminLoginLogPo.setGroupValue(groupValue);
        adminLoginLogPo.setStartTime(startTime);
        adminLoginLogPo.setEndTime(endTime);
        if ("".equals(groupType)){
            adminLoginLogPo.setOrderType("创建时间");
        }else{
            adminLoginLogPo.setOrderType(orderType);
        }
        adminLoginLogPo.setOrderValue(orderValue);
        Workbook wb = null;
        String s = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        List<AdminLoginLogPo> list = adminLogService.getResNotPaged_login(adminLoginLogPo);
        List<Map<String, Object>> maps = BeanToMapUtils.convertListBean2ListMap(list, AdminLoginLogPo.class);

//        定义表头字段


        if ("".equals(groupType)){
            String[] excelHeader = {"用户ID","姓名","ip地址", "创建时间"};
            String[] excelHeaderKey = {"userid","userName","IPaddress", "time"};
            wb = ExcelUtils.exportExcel(excelHeader, excelHeaderKey, maps, s+"登录统计");
        }
        else if ("worknum".equals(groupType)){
            String[] excelHeader = {"用户ID","姓名","合计"};
            String[] excelHeaderKey = {"userid","userName","count"};
            wb = ExcelUtils.exportExcel(excelHeader, excelHeaderKey, maps, s+"登录统计");
        }
        else{
            String[] excelHeader = {"IP","合计"};
            String[] excelHeaderKey = {"IPaddress","count"};
            wb = ExcelUtils.exportExcel(excelHeader, excelHeaderKey, maps, s+"登录统计");
        }

        String title = s+"登录统计.xlsx";
        ExcelUtils.setExcelResponse(response, title, wb);
    }


    /*****************************关键操作日志*******************************/
    @ApiOperation(value = "获取操作类型")
    @GetMapping("/GetOperateType")
    public Result GetOperateType(){
        MonitorTypeTool obj = new MonitorTypeTool();
        return Result.succ(obj);
    }




    @ApiOperation(value = "查看关键操作日志")
    @GetMapping("/MonitorLogList")
    public Result MonitorLogList(
            @PathParam("operateType") String operateType,
            @PathParam("operater") String operater,
            @PathParam("startTime") String startTime,
            @PathParam("endTime") String endTime,
            @PathParam("orderType") String orderType,
            @PathParam("orderValue") String orderValue,
            @PathParam("page") int page,
            @PathParam("pageSize") int pageSize
    ) {
        MonitorBo bo = new MonitorBo();
        bo.setOperateType(operateType);
        bo.setUserid(operater);
        bo.setStartTime(startTime);
        bo.setEndTime(endTime);
        bo.setOrderType(orderType);
        bo.setOrderValue(orderValue);
        bo.setPage(page);
        bo.setPageSize(pageSize);
        bo.setStartPage((page-1)*pageSize);
        CutPageBean<MonitorPo> cutPageBean = adminLogService.cutPage_Monitor(bo);
        return Result.succ(cutPageBean);

    }

    @ApiOperation(value = "下载监视日志")
    @GetMapping("/exportMonitorLog")
    public void exportMonitorLog(
            @PathParam("operateType") String operateType,
            @PathParam("operater") String operater,
            @PathParam("startTime") String startTime,
            @PathParam("endTime") String endTime,
            @PathParam("orderType") String orderType,
            @PathParam("orderValue") String orderValue,
            HttpServletResponse response
    ) throws Exception {


        MonitorBo bo = new MonitorBo();
        bo.setOperateType(operateType);
        bo.setUserid(operater);
        bo.setStartTime(startTime);
        bo.setEndTime(endTime);
        bo.setOrderType(orderType);
        bo.setOrderValue(orderValue);

        Workbook wb = null;
        String s = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        List<MonitorPo> list = adminLogService.exportMonitorLog(bo);
        List<Map<String, Object>> maps = BeanToMapUtils.convertListBean2ListMap(list, MonitorPo.class);

//        定义表头字段

        String[] excelHeader = {"用户工号","姓名","操作类型","时间","详情"};
        String[] excelHeaderKey = {"userid","name","operateType","time","desc"};
        wb = ExcelUtils.exportExcel(excelHeader, excelHeaderKey, maps, s+"关键操作统计");

        String title = s+"关键操作统计.xlsx";
        ExcelUtils.setExcelResponse(response, title, wb);
    }

    //新建日志统计
    @PostMapping(value = "InsertLog")
    public Result insertZY(@RequestBody AdminViewDownLogPo adminViewDownLogPo,HttpServletRequest httpServletRequest){
        String userId=   TokenUtils.getCurrentUserId(httpServletRequest);
        String createTime = SysTool.getCurrentTime(SysTool.dateFormat_milli);
        adminViewDownLogPo.setUserID(userId);
        adminViewDownLogPo.setCreateTime(createTime);
        Boolean res =  adminLogService.insertLogRecord(adminViewDownLogPo);
        if (res){
            return  Result.succ("存入日志成功");
        }else{
            return  Result.succ("存入日志失败");
        }
    }
}
